#!/bin/bash

interface=wlan0
config_file=/recalbox/share/system/recalbox.conf
config_file_boot=/boot/recalbox-backup.conf
wpa_file=/etc/wpa_supplicant.conf

# if /recalbox/share is not yet mounted
if ! [ -f "$config_file" ]; then
  # use the boot version of the file
  config_file="$config_file_boot"
fi

mask2cidr() {
    nbits=0
    IFS=.
    for dec in $1 ; do
        case $dec in
            255) let nbits+=8;;
            254) let nbits+=7;;
            252) let nbits+=6;;
            248) let nbits+=5;;
            240) let nbits+=4;;
            224) let nbits+=3;;
            192) let nbits+=2;;
            128) let nbits+=1;;
            0);;
            *) echo "Error: $dec is not recognised"; exit 1
        esac
    done
    echo "$nbits"
}

# Function to create wifi profiles based on user settings
rb_wifi_configure() {
  [ "$1" = "1" ] && X="" || X="$1"
  settings_ssid=`egrep -m1 "^wifi${X}.ssid" ${config_file}|sed "s/wifi${X}.ssid=//g"`
  settings_key=`egrep -m1 "^wifi${X}.key" ${config_file}|sed "s/wifi${X}.key=//g"`
  settings_ip=`egrep -m1 "^wifi${X}.ip" ${config_file}|sed "s/wifi${X}.ip=//g"`
  settings_gateway=`egrep -m1 "^wifi${X}.gateway" ${config_file}|sed "s/wifi${X}.gateway=//g"`
  settings_netmask=`egrep -m1 "^wifi${X}.netmask" ${config_file}|sed "s/wifi${X}.netmask=//g"`

  # setup wpa_supplicant network
  if [[ "$settings_ssid" != "" ]] ;then

    recallog "Configuring wifi for SSID: $settings_ssid"
    network=`wpa_cli -i ${interface} add_network`
    wpa_cli -i "$interface" set_network "$network" ssid "\"$settings_ssid\"" || exit 1
    wpa_cli -i "$interface" set_network "$network" psk "\"$settings_key\"" || exit 1
    wpa_cli -i "$interface" set_network "$network" scan_ssid 1 || exit 1
    wpa_cli -i "$interface" enable_network "$network" || exit 1

    # static ip configuration in dhcpcd.conf
    mount -o remount,rw /
    sed -i "/\b\($interface\|static\)\b/d" /etc/dhcpcd.conf
    if [[ "$settings_ip" != "" ]] && \
      [[ "$settings_gateway" != "" ]] && \
      [[ "$settings_netmask" != "" ]] ;then
      recallog "static ip configuration"
      settings_netmask=$(mask2cidr $settings_netmask)
      recallog "static ip_address=$settings_ip/$settings_netmask"
      recallog "static routers=$settings_gateway"
      echo "interface $interface" >> /etc/dhcpcd.conf
      echo "static ip_address=$settings_ip/$settings_netmask" >> /etc/dhcpcd.conf
      echo "static routers=$settings_gateway" >> /etc/dhcpcd.conf
    fi
    mount -o remount,ro /

  fi

}

# function to drop all of the wpa_suppliment networks
wpa_drop_all_networks() {
  # limit any infinite loop to 10 max
  for i in {1..10}; do

    # get the last network id
    netid=`wpa_cli -i $interface list_networks | tail -n1` 
    netid=$(echo $netid | tr " " "\n")

    # exit at the header record
    if [[ $netid == network* ]]; then
      break
    fi

    # remove network id
    wpa_cli -i "$interface" remove_network "$netid" || exit 1
  done
}

enable_wifi() {
  # enable interface
  ifconfig "$interface" up

  # start wpa_supplicant
  killall -q wpa_supplicant
  sleep 2 # prevents a race condition on killall
  wpa_supplicant -B -i "$interface" -c "$wpa_file"

  # clear wpa_supplicant configuration and rebuild
  wpa_drop_all_networks
  wpa_cli -i "$interface" set update_config 1

  settings_region=`egrep "^wifi.region" ${config_file}|sed "s/wifi.region=//g"`
  wpa_cli -i "$interface" set country "$settings_region"

  # iterate through all network ...
  for i in {1..3}; do
    rb_wifi_configure $i
  done

  # write wpa_supplicant configuration
  mount -o remount,rw /
    wpa_cli -i "$interface" save_config
  mount -o remount,ro /
}

start() {
  # turn on rf signals
  if [ -c /dev/rfkill ]; then
    rfkill unblock all
  fi

  if [ -f "$config_file" ]; then
    if [ `egrep "^wifi.enabled" ${config_file}|sed "s/wifi.enabled=//g"` -ne 0  ]; then
      recallog "Wifi option enabled, trying to configure"
      sleep 2 # wait a bit to be sure tmpfs is initialized
      enable_wifi
    else
      recallog "Wifi option disabled"
      stop
    fi
  else
    recallog "${config_file} not found"
  fi

  # If share over network, let's add a timeout of 10 seconds (max) for slow networks
  if [ `grep -c "^sharedevice=NETWORK$" ${config_file}` -ne 0 ]; then
    try=0
    until [ ! $try -lt 10 ]
    do
      if [ `ifconfig |grep -v "127.0.0.1"|grep -c "inet addr"` -gt 0 ]; then
        break # Of course if network is OK, don't wait anymore!
      fi
      sleep 1
      try=`expr $try + 1`
    done
  fi
}

stop() {
  # stop wpa_supplicant
  killall -q wpa_supplicant

  # disable interface
  ifconfig "$interface" down
}

# Main
case "$1" in
  start)
    start
	;;
  stop)
    stop
	;;
  restart|reload)
    stop
    start
	;;
  *)
    echo "Usage $0 {start|stop|restart}"
    exit 1
esac

exit $?
